package hyl.base.net.udp;

import hyl.base.net.IData1;
import hyl.core.MyFun;
import hyl.core.fun.MyByte;

public class UdpData implements IData1<byte[]> {

	// 0:未知(可以丢弃); 1:消息; 2:命令
	byte B类型 = B类型_未知;
	byte[] data;
	String ip;
	int port;

	public String getIp() {
		return ip;
	}

	public UdpData setIp(String ip) {
		this.ip = ip;
		return this;
	}

	public int getPort() {
		return port;
	}

	public UdpData setPort(int port) {
		this.port = port;
		return this;
	}

	public UdpData setIpPort(String ip, int port) {
		this.ip = ip;
		this.port = port;
		return this;
	}

	public UdpData setB类型(byte 类型) {
		this.B类型 = 类型;
		return this;
	}

	public UdpData setData(byte[] data) {
		this.data = data;
		return this;
	}

	public UdpData receiveData(byte[] 收到数据, int len) {
		B类型 = 收到数据[0];
		this.data = MyByte.subBytes(收到数据, 1, len - 1);
		return this;

	}

	@Override
	public byte[] getSendBytes() {
		return MyFun.concat(new byte[] {B类型 }, data);
	}

	@Override
	public byte getB类型() {
		return this.B类型;
	}

	@Override
	public byte[] getData() {
		return data;
	}

	public String toString() {
		return MyFun.join("{\"ip\":\"", this.ip, "\",\"port\":", this.port, ",\"协议\":", this.B类型, ",\"data\":\"",
				new String(data), "\"}");
	}
}
